package img;

import util.FileCreator;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 16:20:35 2018/11/13
 * @version:2.0
 */
public class ImgUtil {
    //获取单一纯色像素
    public static BufferedImage selectGRB(BufferedImage bfImage, int target) {
        int width = bfImage.getWidth();
        int height = bfImage.getHeight();
        int[][] result = new int[width][height];
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                //使用getRGB(w, h)获取该点的颜色值是ARGB，而在
                // 实际应用中使用的是RGB，所以
                // 需要将ARGB转化成RGB，即bufImg.getRGB(w, h) & 0xFFFFFF。
                int color = bfImage.getRGB(i, j) & 0xFFFFFF;
                int r = (color & 0xff0000) >> 16;
                int g = (color & 0xff00) >> 8;
                int b = (color & 0xff);
                if ((r - g) > target && (r - b) > target) {
                    r = 255;
                    g = 0;
                    b = 0;
                } else if (g - r > target && g - b > target) {
                    r = 0;
                    g = 255;
                    b = 0;
                } else if (b - g > target && b - r > target) {
                    r = 0;
                    g = 0;
                    b = 255;
                } else {
                    r = 255;
                    g = 255;
                    b = 255;
                }
                int colorNew = (r + g + b);

                result[i][j] = (r << 16) | (g << 8) | b;
                bi.setRGB(i, j, result[i][j]);
            }
        }
        return bi;
    }

    //保存图片
    public static void savePic(BufferedImage bi, String path) {
        try {
            ImageIO.write(bi, "JPEG", FileCreator.checkExist(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //切割图片  430以下  底部46以上 左267 又163；
    public static BufferedImage devideImg(int[][] pixels, int top, int left, int bottom, int right) {
        int[][] pixelsNew = PixelsUtil.getPixelsInFiled(pixels, top, left, bottom, right);
        int width = pixelsNew.length;
        int height = pixelsNew[0].length;
        BufferedImage bi = new BufferedImage(pixelsNew.length, pixelsNew[0].length, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                //color = color > 128 ? 255 : 0;
                bi.setRGB(i, j, pixelsNew[i][j]);
            }
        }
        return bi;
    }

    /**
     * 将彩色图片变为灰色的图片
     */
    public static BufferedImage makeImageColorToBlackWhite(ImgFile img) {
        int[][] result = img.pixels;
        int[] rgb = new int[3];
        int width = result.length;
        int height = result[0].length;
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                rgb[0] = (result[i][j] & 0xff0000) >> 16;
                rgb[1] = (result[i][j] & 0xff00) >> 8;
                rgb[2] = (result[i][j] & 0xff);
                int color = (int) (rgb[0] * 0.3 + rgb[1] * 0.59 + rgb[2] * 0.11);
                //color = color > 128 ? 255 : 0;
                bi.setRGB(i, j, (color << 16) | (color << 8) | color);
            }
        }
        savePic(bi, img.path + "black_" + img.fileName);
        return bi;
    }

    /**
     * 颜色分量转换为RGB值
     */
    private static int colorToRGB(int alpha, int red, int green, int blue) {
        int newPixel = 0;
        newPixel += alpha;
        newPixel = newPixel << 8;
        newPixel += red;
        newPixel = newPixel << 8;
        newPixel += green;
        newPixel = newPixel << 8;
        newPixel += blue;
        return newPixel;

    }

    /**
     * 二值化-黑白化*
     */
    public static BufferedImage binaryImage(BufferedImage image, double SW) {
        int w = image.getWidth();
        int h = image.getHeight();
        float[] rgb = new float[3];
        double[][] zuobiao = new double[w][h];
        int black = new Color(0, 0, 0).getRGB();
        int white = new Color(255, 255, 255).getRGB();
        BufferedImage bi = new BufferedImage(w, h,
                BufferedImage.TYPE_BYTE_BINARY);
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                int pixel = image.getRGB(x, y);
                rgb[0] = (pixel & 0xff0000) >> 16;
                rgb[1] = (pixel & 0xff00) >> 8;
                rgb[2] = (pixel & 0xff);
                float avg = (rgb[0] + rgb[1] + rgb[2]) / 3;
                zuobiao[x][y] = avg;
            }
        }
        //这里是阈值，白底黑字还是黑底白字，大多数情况下建议白底黑字，后面都以白底黑字为例
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                if (zuobiao[x][y] < SW) {
                    bi.setRGB(x, y, white);
                } else {
                    bi.setRGB(x, y, black);
                }
            }
        }

        return bi;
    }


}
